#include <bits/stdc++.h>
using namespace std;

#define DEBUG
#ifdef DEBUG
#define TRG_PG(str, argv...) fprintf(stderr, "  TRC_PG(%s:%d):\t" str, __func__, __LINE__, ##argv)
#define debug fprintf(stderr, "DEBUG(%s:%d)\n", __func__, __LINE__)
#else
#define TRG_PG(str, argv...)
#define debug
#endif

const int MAX_N = 1e5 + 10;

int d[20][3 * MAX_N], ans[20][3 * MAX_N];
int a[MAX_N], a2[MAX_N], c[MAX_N];
int n, m;

void input()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> c[i];
    }
    int k = ceil(log2(n));
    for (int i = 1; i <= k; i++)
    {
        string str;
        cin >> str;
        for (int j = 1; j <= (1 << (k - i)); j++)
        {
            d[i][j] = str[j];
        }
    }
}

void work()
{
    int T;
    cin >> T;
    while (T-- > 0)
    {
        int x[4];
        cin >> x[0] >> x[1] >> x[2] >> x[3];
        for (int i = 1; i <= n; i++)
        {
            a2[i] = a[i] ^ x[i % 4];
            ans[0][i] = i;
        }
        int k = ceil(log2(n));
        for (int i = 1; i <= k; i++)
        {
            for (int j = 1; j <= (1 << (k - i)); j++)
            {
                if (a2[ans[i - 1][2 * j + d[i][j] - 1]] >= i)
                {
                    ans[i][j] = ans[i - 1][2 * j + d[i][j] - 1];
                }
                else
                {
                    ans[i][j] = ans[i - 1][2 * j + !d[i][j] - 1];
                }
            }
            cout << ans[i][0];
        }
        long long ANS = 0;
        for (int i = 1; i <= m; i++)
        {
            cout << log2(c[i]) << "\n";
            ANS ^= 1ll * i * ans[(int)log2(c[i])][1];
        }
        cout << ANS << '\n';
    }
}

int main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    input();
    work();
    return 0;
}
